Repository navigation
fix(migrate): 0.14 blob@, attach@ and filepath@ columns read in 2.x after migration - #1753
Open
dimitri-yatsenko wants to merge 6 commits into
Open
dimitri-yatsenko wants to merge 6 commits into
dimitri-yatsenko wants to merge 6 commits into
Conversation
…fter migration
migrate_external wrote a url built from a NULL filepath, the 32-digit hex
hash, no path or store, and a :blob@store: comment that the heading does not
read. It now writes {hash, path, schema, store, size}, with the path where
0.14 stored the file and the same MD5 digest in base32, and a :<codec@store>:
comment. Migrated attachments record their name, which get_hash puts back
before checking the hash, as 0.14 hashed it.
migrate_filepath matches 0.14's binary(16) columns and writes filepath
metadata from the tracking table. migrate_external_pointers_v2 writes the
same metadata and refuses a binary(16) column. verify_external_integrity
derives blob and attachment paths, and verify_schema_v20 escapes % in LIKE.
Every referenced file is looked up before anything changes; a missing file
stops the migration with the schema unchanged.
Fixes #1698
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
November 23, 2026 23:09
…runs; keep NOT NULL - copy_table_data converts 0.14 in-store columns when it copies a 0.14 table into a table declared by 2.x, so the guide's Option A works end to end. Every file is looked up first; a missing one stops the copy with nothing copied. - migrate_external_pointers_v2 converts one 0.14 column in place: it adds the JSON column, fills it, and swaps it in with a 2.x comment that names dest_store. It refuses a column already declared by 2.x, pointing to copy_table_data. - Column comments are quoted and escaped in every ALTER, including apostrophes, backslashes, and percent signs. - If a step fails, the _v2 columns the run wrote are dropped again. - A rerun rewrites _v2 columns written by an earlier release, whose values 2.x cannot read, and repairs columns an earlier release finalized. - Finalize checks every _v2 column first, renames both columns in one statement, and keeps the original nullability. - Tests for each of these, for filepath rows without contents_hash, and for a dry run without the store configured.
… subfolding - migrate_external, migrate_external_pointers_v2, copy_table_data and verify_external_integrity take legacy_database, the database 0.14 wrote the files from. 0.14 put blob and attachment files under that name, so a copy of a 0.14 schema is migrated by naming the original. copy_table_data defaults to the source schema. A refusal for missing files says so. - create_parallel_schema creates and fills tables with foreign key checks off for the session, so tables that come before the tables they reference (such as the 0.14 ~external_* tables, which sort last) are created. Comments with % are copied. - Garbage collection never deletes migrated 0.14 files in the root-level layout (schema_prefix=""): hash references count as covering, files named as 0.14 named blobs and attachments are never orphans, and the section of a schema that still has ~external_* tables is not listed. - "subfolding": null, as the configuration template writes it, means unset, so 0.14's default (2, 2) applies. - Repair keeps a column nullable when an earlier release's finalize made it so, says so in the dry run and the log, and the docstring explains how to restore NOT NULL.
…lepath@> references
In the root-level layout (schema_prefix=""), a file directly under {schema}/
is never an orphan: 2.x schema-addressed paths are {schema}/{table}/..., so such
a file is a 0.14 file stored without subfolding or a blob 0.14 had migrated from
0.11, which a copy migrated with legacy_database may reference. GC also reads
<filepath@> references of the collected schemas in this layout. The 0.14 file
rules and hash-reference coverage now apply only to the root-level layout.
A <filepath@> value is stored as written, so './{db}/sub/a.txt' and
'{db}//sub/b.txt' read the file at '{db}/sub/a.txt' but did not match the
listed path, and collect(dry_run=False) deleted it. Normalize every
reference path before matching (backslashes to '/', posixpath.normpath,
leading '/' stripped), as <filepath@> does for its reserved-section check.
Qualify the GarbageCollector *schemas docstring and the
filepath_references docstring for the root-level layout.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
After the documented migration of a 0.14 schema, columns declared
blob@store,attach@store, orfilepath@storecould not be fetched in 2.x, although the migration reported success. This PR makes both documented paths write values that 2.x reads: Option A (copy into a 2.x schema) and Option B (in place). It also fixes the related defects listed in the issue, and two defects found along the way:create_parallel_schemacould not copy a schema whose tables reference tables that sort after them, and garbage collection could delete migrated 0.14 files when a store uses the root-level layout.Fixes #1698
Docs: datajoint/datajoint-docs#319
Changes
The 0.14 layout comes from
external.py,hash.py, anddeclare.py. I checked it fromv0.14.6throughv0.14.10: paths and hashes are the same in every release.Metadata that 2.x reads
migrate_externalwrites{hash, path, schema, store, size}.{database}/{subfolders}/{hex digest}. For attachments,.{attachment name}is appended.{database}is the database 0.14 wrote the file from. For a copy of a 0.14 schema, that is the original database, not the copy (see below).subfoldingsetting, or 0.14's default(2, 2)when the store has none."subfolding": null, which the 2.x configuration template writes, counts as none. An explicit list wins, including[]for a 0.14 store that turned subfolding off.filepath, and the hash of their contents incontents_hash. These are used, as 0.14 did.<attach>stores. Migrated attachments recordattachment_name, andget_hashputs the name back in front of the contents before checking the hash. This is 0.14's own hash, so a changed file or name fails the check.migrate_filepathmatches 0.14'sbinary(16)filepath columns. It writes{path, store, size, is_dir, timestamp}from the tracking table. From 0.14.7, filepath rows can have a NULLcontents_hash(large files skip the checksum). These rows migrate like any other, because 2.x does not need it.:<blob@store>:,:<attach@store>:, or:<filepath@store>:, followed by the rest of the 0.14 comment.ALTER. A 0.14 comment with an apostrophe, a backslash, or a%now migrates and keeps its text.Copies of a 0.14 schema
migrate_external,migrate_external_pointers_v2,copy_table_data, andverify_external_integritytakelegacy_database: the name of the database 0.14 wrote the files from. It defaults to the schema being migrated (forcopy_table_data, tosource_schema). On a copy, such as one made withcreate_parallel_schema(..., copy_data=True), pass the original name. Without it, the files are not found and the call refuses with nothing changed; the error says to passlegacy_database. Filepath paths are stored in full in the tracking table, somigrate_filepathneeds no such parameter.create_parallel_schemacreates tables in name order, so a table could come before the tables its foreign keys reference, such as the 0.14~external_*tables, which sort last. On master this fails with MySQL error 1824. It now turns foreign key checks off for its session while it creates and fills the tables, and restores the previous setting afterwards. The source rows already satisfy the constraints. References to tables in the same database are unqualified inSHOW CREATE TABLE, so the copy's foreign keys point to the copy's own tables (tested). Comments with%are now copied.Option A: copy into a 2.x schema
copy_table_dataconverts in-store columns when it copies a 0.14 table into a table declared by 2.x. A 0.14blob@storecolumn must be declared<blob@store>there (likewiseattach,filepath). The 2.x store must point to the 0.14 location. Files stay where they are. The rows are copied in one statement. Tables without 0.14 in-store columns are copied exactly as before.migrate_external_pointers_v2converts one 0.14 column in place, for a table still in the 0.14 layout. It adds a JSON column, fills it, and swaps it in, keeping the original as<name>_v1. The new comment namesdest_store. On a column already declared by 2.x, it raises and points tocopy_table_data. Before, it wrote JSON that 2.x read as raw bytes, or failed withData too long.Option B: in place
_v2columns the run wrote are dropped again, and the original columns are unchanged._v2columns written by an earlier 2.x release, whose values 2.x cannot read. Before, it skipped them. It also repairs columns that an earlier release finalized, from the references kept in<name>_v1. Columns that already read are skipped.NOT NULL. An earlier release's finalize made every column nullable and did not keep the 0.14 setting, so a repaired column stays nullable. The dry run marks these columns withnullable: True, and the run logs a warning. Where the column was required, restore it by hand in SQL, for exampleALTER TABLE `db`.`recording` MODIFY COLUMN `signal` JSON NOT NULL COMMENT ':<blob@store>:raw signal'(tested), keeping the column's comment.alter()cannot do this while the table keeps itsbinary(16)<name>_v1columns, because 2.x definitions do not accept that type._v2column exists and reads, and changes nothing otherwise. It renames both columns in one statement. The migrated column keeps the original's nullability. Before, a required column became optional.Garbage collection
Migrated 0.14 files stay where 0.14 stored them, under
{database}/. With the defaultschema_prefix(_schema), garbage collection never walks there. Withschema_prefix=""(the root-level layout for stores written by 2.3.0 and earlier),{database}/is the schema section, and on master GC lists the migrated files as orphans andcollect(dry_run=False)deletes them.<filepath@>files may lie there too, and master does not read<filepath@>references. In the root-level layout only, GC now keeps these files:<blob@>or<attach@>value) or a<filepath@>reference points to counts as referenced;.and a name) is never an orphan;{schema}/, with no subfolder, is never an orphan. 2.x schema-addressed paths are{schema}/{table}/..., so such a file is a 0.14 file stored without subfolding or a blob that 0.14 had migrated from 0.11;~external_*tracking tables is not listed at all. The schema is listed under the newskipped_schemaskey of the report, and a warning is logged.A
<filepath@>value is stored as written, so values like./{schema}/sub/a.txtor{schema}//sub/b.txtread the same file as{schema}/sub/a.txt. In the root-level layout, GC normalizes every reference path (backslashes turned into/, thenposixpath.normpath, leading/stripped) before matching it with the listed files, the same normalization<filepath@>uses for its reserved-section check. Such values now count as references to the files they read.GC reads references only from the schemas it collects, in the store it collects. A file in a subfolder of
{schema}/that is not named as 0.14 named its files, and that only rows of other schemas reference (for example a<filepath@>file referenced from another schema), is still listed as an orphan. Pass every schema whose rows reference files in the store, such as a copy migrated withlegacy_database, to the same collector, and preview withdry_run=Truebefore deleting. Thegc.pymodule,GarbageCollector,filepath_referencesandcollectdocstrings and both docs pages state the same; the class docstring's "pass any subset safely" and the docs "Multiple Schemas" section name the root-level layout as the exception.On master, a table or column whose references cannot be read is logged and skipped, so files that only it references would be listed as orphans. #1689 records those failures as scan errors, which make
collect(dry_run=False)refuse to delete. The<filepath@>references added here are read through the same method, so with #1689 a failed reference read,<filepath@>included, blocks deletion. This PR does not repeat that change.Nothing changes for stores with a
schema_prefix.Other fixes
verify_external_integrityderives the path for blobs and attachments, whosefilepathis NULL.verify_schema_v20escapes%in itsLIKEpattern.dry_run=False.Behavior changes
migrate_external,migrate_filepath,migrate_external_pointers_v2, andcopy_table_dataraise and change nothing. A dry run lists these references underproblems.migrate_external_pointers_v2now changes the column type to JSON and renames the original to<name>_v1. Before, it wrote into the existing column.create_parallel_schemaruns with foreign key checks off for its session.GarbageCollector.collectreportsskipped_schemas. In the root-level layout it reads<filepath@>references of the collected schemas and keeps the files described above.Not changed
migrate_external_pointers_v2does not copy files (copy_files=Trueonly logs a warning, as before).{schema}/, not named as 0.14 named its files, that only rows of other schemas reference can be listed as an orphan (see Garbage collection above).Tests
tests/integration/test_migrate_legacy_external.pylays out a schema as 0.14 does, on MySQL. It has a~external_legacytable,binary(16)columns with 0.14 comments and foreign keys, and files at 0.14's paths. The blobs were serialized by 0.14.6's ownblob.pack. It checks thatfetch1returns the original arrays, attachment, and file after:_v2columns and after finalizing, with nullability kept;copy_table_datainto a 2.x declaration, includingwhere_clauseandlimit;migrate_external_pointers_v2on abinary(16)column with a new store name, and on a widened column;create_parallel_schema(..., copy_data=True), withlegacy_databasenaming the original. Without it, each refuses and nameslegacy_database.create_parallel_schemacopies the 0.14 schema with its foreign keys pointing into the copy, restores foreign key checks, and keeps%in comments;schema_prefix="",collect(dry_run=False)deletes no migrated 0.14 file: neither while the tracking table exists (the schema is skipped) nor after it is dropped (only an unrelated stray file is removed; a 0.14 blob no row references, and the blob migrated from 0.11, stay);schema_prefix="", collecting the original of a copy migrated withlegacy_database, after the original's 0.14 tables are dropped, keeps every file only the copy references, and the copy still reads;"subfolding": nullfinds the files at 0.14's default paths;NOT NULL;%, and a backslash migrate and keep their text;_v2columns already written;_v2columns and finalized columns written by an earlier release are rewritten and then read;_v2column is missing;contents_hashmigrates;<attach@>on MySQL and PostgreSQL.schema_prefix="",<filepath@>values./{db}/sub/a.txtand{db}//sub/b.txtare not listed as orphans, survivecollect(dry_run=False), and still read. Without the normalization, both files are deleted.schema_prefix="", a copy's migrated-style values point under the original's name. Collecting the original alone keeps the 0.14-named file, the file directly under{schema}/, and the original's own<filepath@>file, and lists a file in a subfolder that only the copy's<filepath@>column references. Collecting both schemas keeps it;collect(dry_run=False)removes only a stray file, and every value still reads.tests/unit/test_migrate_legacy_store.pycovers the paths, the hash conversion, subfolding (includingnull), the attachment read, comment escaping, and theverify_schema_v20query.gc.py,collect(dry_run=False)deletes the migrated 0.14 files (the two copy tests also fail with thegc.pyof this PR's earlier commits, which did not keep files directly under{schema}/or read<filepath@>references); with master'screate_parallel_schema, copying the 0.14 schema fails with MySQL error 1824. Thecontents_hashand dry-run tests pin behavior that is correct in the new code paths.test_gc.py,test_attach.py,test_hash_storage.py,test_object.py,test_blob.py,test_migrate_job_metadata.py) pass locally on MySQL and PostgreSQL.Merge order
GarbageCollector._references; whichever merges second resolves the overlap, keeping the scan-error recording for the<filepath@>scan too.For review
These are fail-closed choices I made. Please confirm or redirect:
copy_table_data, andmigrate_external_pointers_v2swaps the column in place. The alternative is to send Option A users tomigrate_externalon a copy of the schema, which now works withlegacy_database.legacy_database), not detected. A copy records nothing about its source, so the call refuses until the name is given.create_parallel_schematurns foreign key checks off for its session instead of ordering tables by dependency.{schema}/. Some files that are truly orphaned are therefore kept; they can be removed by hand._v2columns by dropping and re-adding them. Their values came from the original columns, which are never changed.